package com.mutual.system.areaInfo.controller;



import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.mutual.appUserInfo.pojo.AppUserInfo;
import com.mutual.appUserInfo.service.AppUserInfoService;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.commom.util.SystemUtil;
import com.mutual.common.BaseController;
import com.mutual.common.ResultList;
import com.mutual.system.areaInfo.pojo.AreaInfo;
import com.mutual.system.areaInfo.service.AreaInfoService;
import com.mutual.system.user.pojo.User;
import com.mutual.system.user.service.UserService;

@Controller
@RequestMapping("/area")
public class AreaInfoController extends BaseController{

	@Resource
	private AreaInfoService areaInfoService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private AppUserInfoService appUserInfoService;
	
	private static Logger logger = Logger.getLogger (AreaInfoController.class);
	
	@RequestMapping(value = "/toTreeMain")
	public String list(AreaInfo areaInfo,Model model){
		areaInfo.setGrade(AreaInfo.AreaInfoGradeEnum.GRADE_PROVICE.getValue());//默认查询出来省
		areaInfo.setState(AreaInfo.AreaInfoStateEnum.STATE_YES.getValue());//可用的
		List<AreaInfo> areaInfoList=areaInfoService.findAreaInfoList(areaInfo);
//		areaInfoList.forEach(item->{
//			item.setParent(true);
//		});
		model.addAttribute("areaListJson", JSONObject.toJSON(areaInfoList).toString());
		return "system/areaInfo/treeMain";
	}
	
	
	/**
	 * 区域树展示
	 * @param areaInfo
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toAreaTree")
	public String toAreaTree(AreaInfo areaInfo,Model model){
		areaInfo.setGrade(AreaInfo.AreaInfoGradeEnum.GRADE_PROVICE.getValue());//默认查询出来省
		areaInfo.setState(AreaInfo.AreaInfoStateEnum.STATE_YES.getValue());//可用的
		List<AreaInfo> areaInfoList=areaInfoService.findAreaInfoList(areaInfo);
		model.addAttribute("areaListJson", JSONObject.toJSON(areaInfoList).toString());
		return "areaTree/areaTree";
	}
	
	/**
	 * 
	 * @param areaInfo
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/selectAreaInfoListByPid")
	@ResponseBody
	public List<AreaInfo> selectAreaInfoListByPid(AreaInfo areaInfo){
		return areaInfoService.findAreaInfoList(areaInfo);
	}
	
	@RequestMapping(value = "/toUserListAreaInfo")
	public String toUserListAreaInfo(@RequestParam Map<String,Object> param,Model model){
		AreaInfo areaInfo=null;
		if(!StringUtils.isEmpty(param.get("areaId"))){
			areaInfo=areaInfoService.selectByPrimaryKey(SystemUtil.getInt(param.get("areaId")));
		}
		model.addAttribute("areaInfo", (areaInfo==null?new AreaInfo():areaInfo));
		return "system/areaInfo/toUserListAreaInfo";
	}
	
	@RequestMapping(value = "/userListData")
	@ResponseBody
	public ResultList<User> userListDaya(@RequestParam Map<String,Object> param){
		if(StringUtils.isEmpty(param.get("areaId"))){
			return new ResultList<>();
		}
		total=userService.selectUserListCount(param);
		if(total==0){
			return new ResultList<>();
		}
		ResultList<User> result=new ResultList<>();
		result.setCount(total);
		List<User> userList=userService.selectUserList(param);
		result.setData(userList);
		return result;
	}
	
	@RequestMapping(value = "/appUserListData")
	@ResponseBody
	public ResultList<AppUserInfo> appUserListData(@RequestParam Map<String,Object> param){
		if(StringUtils.isEmpty(param.get("areaId"))){
			return new ResultList<>();
		}
		total=appUserInfoService.selectAppUserInfoCount(param);
		if(total==0){
			return new ResultList<>();
		}
		List<AppUserInfo> userList=appUserInfoService.selectAppUserInfoList(param);
		ResultList<AppUserInfo> result=new ResultList<>();
		result.setData(userList);
		result.setCount(total);
		return result;
	}
	
	@RequestMapping(value = "/toAddAreaInfo")
	public String toAddAreaInfo(int id,Model model){
		AreaInfo areaInfo=areaInfoService.selectByPrimaryKey(id);
		model.addAttribute("areaInfo", areaInfo);
		return "system/areaInfo/addAreaInfo";
	}
	
	@RequestMapping(value = "/toUpdateAreaInfo")
	public String toUpdateAreaInfo(int id,Model model){
		AreaInfo areaInfo=areaInfoService.selectByPrimaryKey(id);
		AreaInfo parentAreaInfo=areaInfoService.selectByPrimaryKey(areaInfo.getPId());
		model.addAttribute("areaInfo", areaInfo);
		model.addAttribute("parentAreaInfo", parentAreaInfo);
		return "system/areaInfo/updateAreaInfo";
	}
	
	@RequestMapping(value = "/addAreaInfo")
	@ResponseBody
	public boolean addAreaInfo(AreaInfo areaInfo){
		boolean flag=false;
		try {
			areaInfoService.insert(areaInfo);
			flag=true;
		} catch (Exception e) {
			logger.error(e);
		}
		return flag;
	}
	
	@RequestMapping(value = "/updateAreaInfo")
	@ResponseBody
	public boolean updateAreaInfo(AreaInfo areaInfo){
		boolean flag=false;
		try {
			areaInfoService.updateByPrimaryKey(areaInfo);
			flag=true;
		} catch (Exception e) {
			logger.error(e);
		}
		return flag;
	}
	
	@RequestMapping(value = "/deleteAreaInfo")
	@ResponseBody
	public ReturnResult deleteAreaInfo(AreaInfo areaInfo){
		ReturnResult result=null;
		try {
			result=areaInfoService.deleteAreaInfo(areaInfo);
		} catch (Exception e) {
			logger.error(e);
			result.setReturnMsg("删除异常");
			result.setStatus(ReturnResult.STATUS_FAILED);
		}
		return result;
	}
	
	@RequestMapping(value = "/showAreaInfoTreeSelect")
	@ResponseBody
	public Object showAreaInfoTreeSelect(){
		List<AreaInfo> areaInfos=areaInfoService.findAreaInfoList(null);
		List<AreaInfo> provices=areaInfos.stream()
				.filter(item->item.getGrade()==AreaInfo.AreaInfoGradeEnum.GRADE_PROVICE.getValue())
				.collect(Collectors.toList());
		List<AreaInfo> citys=areaInfos.stream()
				.filter(item->item.getGrade()==AreaInfo.AreaInfoGradeEnum.GRADE_CITY.getValue())
				.collect(Collectors.toList());
		List<AreaInfo> subCitys=areaInfos.stream()
				.filter(item->item.getGrade()==AreaInfo.AreaInfoGradeEnum.GRADE_SUB_CITY.getValue())
				.collect(Collectors.toList());
		List<JSONObject> allList=Lists.newArrayList();
		for(AreaInfo p:provices){
			JSONObject pJsonObject=new JSONObject();
			pJsonObject.put("name", p.getName());
			pJsonObject.put("icon", "&#xe6af");
			pJsonObject.put("id", p.getId());
			pJsonObject.put("spread", false);
			pJsonObject.put("code", p.getCode());
			allList.add(pJsonObject);
			List<JSONObject> cChildren=Lists.newArrayList();
			pJsonObject.put("children", cChildren);
			for(AreaInfo c:citys){
				if(SystemUtil.getInt(c.getPId())==SystemUtil.getInt(p.getId())){
					JSONObject cJsonObject=new JSONObject();
					cJsonObject.put("name", c.getName());
					cJsonObject.put("icon", " ");
					cJsonObject.put("id", c.getId());
					cJsonObject.put("spread", false);
					cJsonObject.put("code", c.getCode());
					List<JSONObject> scChildren=Lists.newArrayList();
					cJsonObject.put("children", scChildren);
					for(AreaInfo sc:subCitys){
						if(SystemUtil.getInt(sc.getPId())==SystemUtil.getInt(c.getId())){
							JSONObject scJsonObject=new JSONObject();
							scJsonObject.put("name", sc.getName());
							scJsonObject.put("icon", " ");
							scJsonObject.put("id", sc.getId());
							scJsonObject.put("spread", false);
							scJsonObject.put("code", sc.getCode());
							scChildren.add(scJsonObject);
						}
					}
					cChildren.add(cJsonObject);
				}
			}
		}
		return allList;
	}
}
